// src/stores/materialInbound.ts
import { defineStore } from 'pinia';
import axios from '@/plugins/axios';

// 入库记录数据接口
export interface MaterialInbound {
  id: number;
  inboundNumber: string;
  materialId: number | null;
  materialName: string;
  quantity: number;
  status: string;
  reviewerId: number | null;
  operatorId: number | null;
  operatorName: string;
  inboundTime: string;
  expirationDate: string;
}

// 入库记录表单字段配置
export const inboundFields = [
  {
    key: 'inboundNumber',
    label: '入库编号',
    type: 'text',
    placeholder: '请输入入库编号（如 IN001）',
    maxLength: 10,
  },
  {
    key: 'materialId',
    label: '物资',
    type: 'select',
    placeholder: '请选择物资',
    options: [] as { value: string; label: string }[],
  },
  {
    key: 'quantity',
    label: '数量',
    type: 'number',
    placeholder: '请输入数量',
  },
  {
    key: 'operatorId',
    label: '操作人',
    type: 'select',
    placeholder: '请选择操作人',
    options: [] as { value: string; label: string }[],
  },
  {
    key: 'expirationDate',
    label: '保质期',
    type: 'date',
    placeholder: '请选择保质期',
  },
];

export const useMaterialInboundStore = defineStore('materialInbound', {
  state: () => ({
    inbounds: [] as MaterialInbound[],
  }),
  actions: {
    // 获取入库记录列表
    async fetchInbounds() {
      try {
        const response = await axios.get('/material-inbounds');
        if (response.data.code === 200) {
          this.inbounds = response.data.data;
          return this.inbounds;
        }
        throw new Error(response.data.message || '获取入库记录失败');
      } catch (error) {
        throw new Error('获取入库记录失败');
      }
    },
    // 创建入库记录
    async createInbound(data: {
      inboundNumber: string;
      materialId: number;
      quantity: number;
      operatorId: number;
      expirationDate: string | null;
    }) {
      try {
        const response = await axios.post('/material-inbounds', data);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '创建入库记录失败');
      } catch (error) {
        throw error;
      }
    },
    // 更新入库记录
    async updateInbound(data: {
      id: number;
      inboundNumber: string;
      materialId: number;
      quantity: number;
      operatorId: number;
      expirationDate: string | null;
    }) {
      try {
        const response = await axios.put('/material-inbounds', data);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '更新入库记录失败');
      } catch (error) {
        throw error;
      }
    },
    // 审核入库记录
    async reviewInbound(data: { id: number; status: string; reviewerId: number }) {
      try {
        const response = await axios.put('/material-inbounds/review', data);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '审核入库记录失败');
      } catch (error) {
        throw error;
      }
    },
    // 删除入库记录
    async deleteInbound(id: number) {
      try {
        const response = await axios.delete(`/material-inbounds/${id}`);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '删除入库记录失败');
      } catch (error) {
        throw error;
      }
    },
  },
});